﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Icinetic.FeedCategorySample.Common;
using Icinetic.FeedCategorySample.Entities;
using Icinetic.FeedCategorySample.Services;

namespace Icinetic.FeedCategorySample.ViewModel
{
    public class CoverPageViewModel : BindableBase
    {
        private ObservableCollection<RssItem> _items = new ObservableCollection<RssItem>();
        public virtual ObservableCollection<RssItem> Items
        {
            get { return this._items; }
            set { SetProperty(ref _items, value); }
        }

        private int _selectedIndex = 0;
        public virtual int SelectedIndex
        {
            get { return this._selectedIndex; }
            set { SetProperty(ref _selectedIndex, value); }
        }

        public void LoadItemsList(ObservableCollection<Category> CategoryList)
        {
            Items = new ObservableCollection<RssItem>();
            foreach (Category category in CategoryList) 
            {
                foreach (RssItem item in category.Articles)
                    Items.Add(item);
            }
        }

        public async Task LoadCategoryList()
        {
            ContentService contentService = new ContentService(App.FeedUrl);
            Dictionary<string, List<RssItem>> feedDictionary = new Dictionary<string, List<RssItem>>();
            await contentService.GetItems<RssItem>(feedDictionary);

            List<Category> categoryList = LoadCategories(feedDictionary);
            LoadItemsList(categoryList);
        }

        private List<Category> LoadCategories(Dictionary<string, List<RssItem>> feedDictionary)
        {
            List<Category> categoryList = new List<Category>();

            foreach (var categoryItem in feedDictionary)
            {
                Category category = new Category()
                {
                    Title = categoryItem.Key,
                    ColSpan = 2,
                    RowSpan = 1,
                    Image = "Assets/FondoPortrait.scale-100.png",
                    Articles = ConvertRssItemList(categoryItem.Value)
                };
                categoryList.Add(category);
            }

            return categoryList;
        }

        public void LoadItemsList(List<Category> CategoryList)
        {
            Items = new ObservableCollection<RssItem>();
            foreach (Category category in CategoryList)
            {
                foreach (RssItem item in category.Articles)
                    Items.Add(item);
            }
        }

        private ObservableCollection<RssItem> ConvertRssItemList(List<RssItem> categoryList)
        {
            ObservableCollection<RssItem> auxList = new ObservableCollection<RssItem>();
            foreach (RssItem category in categoryList)
                auxList.Add(category);
            return auxList;
        }
    }
}
